﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models;
using tracker.Models.DTO;
using tracker.Models.DAL;

namespace tracker.Controllers
{
    [CallAjaxFilter]
    public class HandlerLastLocationController : Controller
    {
        //
        // GET: /HandlerLastLocation/

        // DeviceKeySecurity != null: Select one
        // DeviceKeySecurity == null: Select all
        public JsonStringResult Index(String DeviceKeySecurity)
        {
            string result = "";
            if (Session[Common.USER_GROUPID] == null)
            {
                return new JsonStringResult("{logout:1}");
            }
            string currentUserGroupID = Session[Common.USER_GROUPID].ToString();
            string securityCode = Session[Common.USER_SECUTIRY_CODE].ToString();
            List<ELastDeviceInfo> listDeviceInfo = getList(DeviceKeySecurity, currentUserGroupID, securityCode);
            
            result = Utilities.getJsonString(listDeviceInfo, string.Empty, 2);
            return new JsonStringResult(result);

        }
        public List<ELastDeviceInfo> getList(string DeviceKeySecurity, string currentUserGroupID, string securityCode)
        {
            //string currentUserGroupID = Session[Common.USER_GROUPID].ToString();
            List<EUserLogin> lstUser = new List<EUserLogin>();
            UserLoginDAL userDAL = new UserLoginDAL();
            List<ELastDeviceInfo> listDeviceInfo = new List<ELastDeviceInfo>();
            if (currentUserGroupID == "3")
            {
                //Get all
                lstUser = userDAL.getUserLoginByUserAdministrator();
            }
            else if (currentUserGroupID == "4")
            {
                //Get all group
                lstUser = userDAL.getUserLoginByParentCode(securityCode);
            }
            else
            {
                //Get device only
                lstUser = userDAL.getUserLoginBySecurityCode(securityCode);
            }

            if (String.IsNullOrEmpty(DeviceKeySecurity))
            {
                if (lstUser != null)
                {
                    for (int i = 0; i < lstUser.Count; i++)
                    {
                        List<EDevice> lstDevice = new DeviceDAL().getByUserName(lstUser[i].Username);
                        if (lstDevice == null)
                        {
                            continue;
                        }

                        for (int j = 0; j < lstDevice.Count; j++)
                        {
                            ELastDeviceInfo entLastDeviceInfo = new ELastDeviceInfo();
                            TrackLogDAL tlDAL = new TrackLogDAL();
                            ETrackLog entTrackLog = tlDAL.getLastByDeviceKey(lstDevice[j].DeviceKey);
                            ETrackLog entTrackLogAlarm = tlDAL.getLastAlarmByDeviceKey(lstDevice[j].DeviceKey);

                            entLastDeviceInfo.devicetype = lstDevice[j].DeviceTypeID.ToString();
                            if (entTrackLogAlarm != null)
                            {
                                entLastDeviceInfo.alarminfo = entTrackLogAlarm.AlarmCode;
                                entLastDeviceInfo.alarmlatitude = entTrackLogAlarm.Latitude;
                                entLastDeviceInfo.alarmlongitude = entTrackLogAlarm.Longitude;
                                //entLastDeviceInfo.lastalarmtime = entTrackLogAlarm.RecieveTime.convertGMTClientTime().ToString("yyyy-MM-dd hh:mm:ss tt");
                                entLastDeviceInfo.lastalarmtime = entTrackLogAlarm.RecieveTime.ToString("yyyy-MM-dd hh:mm:ss tt");
                                entLastDeviceInfo.alarmspeed = entTrackLogAlarm.Speed;
                                //entLastDeviceInfo.alarmcustomText =  BAlarmDefine.getAlarmNameByAlarmCode(_username, entTrackLogAlarm.AlarmCode);
                                entLastDeviceInfo.alarmcustomText = "";
                            }



                            entLastDeviceInfo.devicestate = "0";
                            // entLastDeviceInfo.geofenceid = 100;
                            entLastDeviceInfo.lastgeofencename = "";
                            entLastDeviceInfo.hasgsmlocation = "";
                            entLastDeviceInfo.RFID = "";
                            entLastDeviceInfo.status = "";
                            entLastDeviceInfo.gpsavailable = false;

                            if (entTrackLog != null)
                            {
                                entLastDeviceInfo.altitude = entTrackLog.Altitude;
                                entLastDeviceInfo.direction = entTrackLog.Direction;
                                entLastDeviceInfo.runtime = entTrackLog.RunTime;
                                entLastDeviceInfo.satellites = entTrackLog.Satellites;
                                entLastDeviceInfo.gsmsignal = entTrackLog.Gsm_Signal;
                                //entLastDeviceInfo.gpstime = entTrackLog.Gps_time.convertGMTClientTime().ToString("yyyy-MM-dd hh:mm:ss tt");
                                entLastDeviceInfo.gpstime = entTrackLog.Gps_time.ToString("yyyy-MM-dd hh:mm:ss tt");
                                entLastDeviceInfo.HDOP = entTrackLog.Hdop;

                                entLastDeviceInfo.journey = entTrackLog.Journey;
                                entLastDeviceInfo.latitude = entTrackLog.Latitude;
                                entLastDeviceInfo.longitude = entTrackLog.Longitude;
                                entLastDeviceInfo.online = entTrackLog.Gps_status;
                                //entLastDeviceInfo.receivedtime = entTrackLog.RecieveTime.convertGMTClientTime().ToString("yyyy-MM-dd hh:mm:ss tt");
                                entLastDeviceInfo.receivedtime = entTrackLog.RecieveTime.ToString("yyyy-MM-dd hh:mm:ss tt");
                                entLastDeviceInfo.speed = entTrackLog.Speed;

                                entLastDeviceInfo.iconname = lstDevice[j].PictureOfDevide;
                                entLastDeviceInfo.devicetype = lstDevice[j].DeviceID.ToString();
                                entLastDeviceInfo.devicename = lstDevice[j].DeviceName;
                                entLastDeviceInfo.sssid = lstDevice[j].DeviceKeySecurity;
                            }
                            listDeviceInfo.Add(entLastDeviceInfo);
                        }
                    }
                }
            }
            else
            {
                EDevice device = new DeviceDAL().getByDeviceKeySecurity(DeviceKeySecurity);


                if (device != null)
                {
                    ELastDeviceInfo entLastDeviceInfo = new ELastDeviceInfo();
                    TrackLogDAL tlDAL = new TrackLogDAL();
                    ETrackLog entTrackLog = tlDAL.getLastByDeviceKey(device.DeviceKey);
                    ETrackLog entTrackLogAlarm = tlDAL.getLastAlarmByDeviceKey(device.DeviceKey);

                    entLastDeviceInfo.devicetype = device.DeviceTypeID.ToString();
                    if (entTrackLogAlarm != null)
                    {
                        entLastDeviceInfo.alarminfo = entTrackLogAlarm.AlarmCode;
                        entLastDeviceInfo.alarmlatitude = entTrackLogAlarm.Latitude;
                        entLastDeviceInfo.alarmlongitude = entTrackLogAlarm.Longitude;
                        //entLastDeviceInfo.lastalarmtime = entTrackLogAlarm.RecieveTime.convertGMTClientTime().ToString("yyyy-MM-dd hh:mm:ss tt");
                        entLastDeviceInfo.lastalarmtime = entTrackLogAlarm.RecieveTime.ToString("yyyy-MM-dd hh:mm:ss tt");
                        entLastDeviceInfo.alarmspeed = entTrackLogAlarm.Speed;
                        //entLastDeviceInfo.alarmcustomText =  BAlarmDefine.getAlarmNameByAlarmCode(_username, entTrackLogAlarm.AlarmCode);
                        entLastDeviceInfo.alarmcustomText = "";
                    }
                    entLastDeviceInfo.devicestate = "0";
                    // entLastDeviceInfo.geofenceid = 100;
                    entLastDeviceInfo.lastgeofencename = "";
                    entLastDeviceInfo.hasgsmlocation = "";
                    entLastDeviceInfo.RFID = "";
                    entLastDeviceInfo.status = "";
                    entLastDeviceInfo.gpsavailable = false;

                    if (entTrackLog != null)
                    {
                        entLastDeviceInfo.altitude = entTrackLog.Altitude;
                        entLastDeviceInfo.direction = entTrackLog.Direction;
                        entLastDeviceInfo.runtime = entTrackLog.RunTime;
                        entLastDeviceInfo.satellites = entTrackLog.Satellites;
                        entLastDeviceInfo.gsmsignal = entTrackLog.Gsm_Signal;
                        //entLastDeviceInfo.gpstime = entTrackLog.Gps_time.convertGMTClientTime().ToString("yyyy-MM-dd hh:mm:ss tt");
                        entLastDeviceInfo.gpstime = entTrackLog.Gps_time.ToString("yyyy-MM-dd hh:mm:ss tt");
                        entLastDeviceInfo.HDOP = entTrackLog.Hdop;

                        entLastDeviceInfo.journey = entTrackLog.Journey;
                        entLastDeviceInfo.latitude = entTrackLog.Latitude;
                        entLastDeviceInfo.longitude = entTrackLog.Longitude;
                        entLastDeviceInfo.online = entTrackLog.Gps_status;
                        //entLastDeviceInfo.receivedtime = entTrackLog.RecieveTime.convertGMTClientTime().ToString("yyyy-MM-dd hh:mm:ss tt");
                        entLastDeviceInfo.receivedtime = entTrackLog.RecieveTime.ToString("yyyy-MM-dd hh:mm:ss tt");
                        entLastDeviceInfo.speed = entTrackLog.Speed;
                        entLastDeviceInfo.State = entTrackLog.State;
                    }
                    entLastDeviceInfo.iconname = device.PictureOfDevide;
                    entLastDeviceInfo.devicetype = device.DeviceID.ToString();
                    entLastDeviceInfo.devicename = device.DeviceName;
                    entLastDeviceInfo.sssid = device.DeviceKeySecurity;
                    
                    listDeviceInfo.Add(entLastDeviceInfo);
                }
            }
            return listDeviceInfo;
        }
    }
}